package com.palnar.ilecker.manager.db;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.palnar.ilecker.constants.IDBConstants;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;



/**
 * The Class DatabaseHelper.
 */
public class DatabaseHelper extends SQLiteOpenHelper implements IDBConstants {

	/** The my data base. */
	private SQLiteDatabase myDataBase;

	/** The my context. */
	private final Context myContext;

	/**
	 * Instantiates a new database helper.
	 * 
	 * @param context
	 *            the context
	 * @param DB_NAME
	 *            the d b_ name
	 */
	public DatabaseHelper(Context context, String DB_NAME) {
		super(context, DB_NAME, null, 1);
		this.myContext = context;
	}

	/**
	 * Creates the data base.
	 * 
	 * @param DB_NAME
	 *            the d b_ name
	 * @throws IOException
	 *             Signals that an I/O exception has occurred.
	 */
	public void createDataBase(String DB_NAME) throws IOException {
		boolean dbExist = checkDataBase(DB_NAME);
		if (dbExist) {
		} else {
			this.getReadableDatabase();
			try {
				copyDataBase(DB_NAME);

			} catch (IOException e) {
				throw new Error("Error copying database" + e.toString());
			}
		}
	}

	/**
	 * Check data base.
	 * 
	 * @param DB_NAME
	 *            the d b_ name
	 * @return true, if successful
	 */
	private boolean checkDataBase(String DB_NAME) {
		SQLiteDatabase checkDB = null;
		try {
			String myPath = DB_PATH + DB_NAME;
			checkDB = SQLiteDatabase.openDatabase(myPath, null,
					SQLiteDatabase.OPEN_READWRITE);
		} catch (SQLiteException e) {
		}

		if (checkDB != null) {
			checkDB.close();
		}

		return checkDB != null ? true : false;
	}

	/**
	 * Copy data base.
	 * 
	 * @param DB_NAME
	 *            the d b_ name
	 * @throws IOException
	 *             Signals that an I/O exception has occurred.
	 */
	private void copyDataBase(String DB_NAME) throws IOException {

		InputStream myInput = myContext.getAssets().open(DB_NAME);
		String outFileName = DB_PATH + DB_NAME;
		OutputStream myOutput = new FileOutputStream(outFileName);
		byte[] buffer = new byte[1024];
		int length;
		while ((length = myInput.read(buffer)) > 0) {
			myOutput.write(buffer, 0, length);
		}
		myOutput.flush();
		myOutput.close();
		myInput.close();
	}

	/**
	 * Open data base.
	 * 
	 * @param DB_NAME
	 *            the d b_ name
	 * @throws SQLException
	 *             the sQL exception
	 */
	public void openDataBase(String DB_NAME) throws SQLException {
		String myPath = DB_PATH + DB_NAME;
		myDataBase = SQLiteDatabase.openDatabase(myPath, null,
				SQLiteDatabase.OPEN_READWRITE);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
	}

	@Override
	public synchronized void close() {
		if (myDataBase != null)
			myDataBase.close();
		super.close();
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}

}
